package com.peas.platform.common.core.configuration;

import cn.hutool.core.date.DatePattern;
import lombok.SneakyThrows;
import org.springframework.format.Formatter;
import org.springframework.lang.NonNull;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class FormatterDate implements Formatter<Date> {

	@SneakyThrows
	@NonNull
	@Override
	public Date parse(@NonNull String text, @NonNull Locale locale) {
		String pattern = DateFormatReg.getPattern(text);
		DateFormat formatter = DateFormatReg.getDateFormat(pattern);
		return formatter.parse(text);
	}

	@NonNull
	@Override
	public String print(@NonNull Date object, @NonNull Locale locale) {
        DateFormat formatter = DateFormatReg.getDateFormat(DatePattern.NORM_DATETIME_PATTERN);
		return formatter.format(object);
	}

}
